package com.junchenrobot.mes.config;

import com.github.lianjiatech.retrofit.spring.boot.log.GlobalLogProperty;
import com.github.lianjiatech.retrofit.spring.boot.log.LoggingInterceptor;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Request;
import okhttp3.Response;
import okio.Buffer;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * 自定义日志拦截器
 *
 * @author SeaWave
 * @date 2022/10/31
 */
@Slf4j
public class LoggerInterrupt extends LoggingInterceptor {
    
    public LoggerInterrupt(
            GlobalLogProperty globalLogProperty) {
        super(globalLogProperty);
    }
    
    
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        log.info("RequestURl: {}", request.url());
        Buffer sink = new Buffer();
        if (request.body() != null) {
            request.body().writeTo(sink);
            log.info("RequestBody: {}", sink.readString(StandardCharsets.UTF_8));
        }
        Response proceed = chain.proceed(request);
        log.info("ResponseCode: {}", proceed.code());
        if (proceed.body() != null) {
            log.info("ResponseBody: {}", proceed.message());
        }
        return proceed;
    }
}
